package pers.jpa.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import pers.jpa.bean.User;
import pers.jpa.service.UserService;

import java.util.*;

/**
 * 控制层
 * @author
 * @date 2021/1/13 0013 - 下午 14:44
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //搜索用户  /findUser?name=test
    @GetMapping("/findUser")
    public List<User> findUser(@RequestParam(value = "name")String name){
        System.out.println("name="+name);
        User user = new User();
        user.setName(name);
        Example<User> example = Example.of(user);
        return userService.getAllByOther(example);
    }

    // 分页显示数据
    @GetMapping("/user")
    public Page<User> showPage(@RequestParam(value = "page") Integer page, @RequestParam(value = "size") Integer size){
        System.out.println("分页： page:"+page+"; size:"+size);
        return userService.getPage(page, size);
    }

    // 排序分页显示数据
    @GetMapping("/user_sort")
    public Page<User> showSortPage(@RequestParam(value = "page") Integer page, @RequestParam(value = "size") Integer size){
        System.out.println("排序分页： page:"+page+"; size:"+size);
        return userService.getPageSort(page, size);
    }
    //保存用户
    @PostMapping("/saveUser")
    public String saveUser(@RequestBody User user){
        userService.savaUser(user);
        return "保存成功！";
    }

    //自定义方法：通过name关键字查找用户
    @GetMapping("/findUserByNameLike")
    public List<User> findByNameLike(@RequestParam(value = "name") String name){
        System.out.println("name ==== " + name);
        return userService.findByNameLike( name );
    }
}
